/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import local.QuestionFacadeLocal;
import model.Question;
import util.JsonReturnMsg;
import util.JsonReturnTable;

/**
 *
 * @author Randy
 */
@WebServlet(name = "QuestionServlet", urlPatterns = {"/QuestionServlet"})
public class QuestionServlet extends HttpServlet {

    @EJB
    QuestionFacadeLocal qFacade;
    Gson gson = new Gson();
    private String json;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");

        try {
            if (action.equals("getQuestions")) {
                getQuestions(request, response);

            } else if (action.equals("updateQuestion")) {
                updateQuestion(request, response);

            } else if (action.equals("createQuestion")) {
                createQuestion(request, response);
            }

        } catch (Exception ex) {
            json = gson.toJson(new JsonReturnMsg("Error", ex.getMessage(), "error"));
            response.getWriter().println(json);
        } finally {
            response.getWriter().close();
        }
    }

    private void getQuestions(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String theme_id = request.getParameter("theme_id");

        ArrayList<Question> qList = new ArrayList<Question>();

        if (!theme_id.equals("")) {
            qList = qFacade.getQuestions(Long.parseLong(theme_id));
        }

        json = gson.toJson(new JsonReturnTable(qList.size() + "", qList));
        response.getWriter().println(json);
    }

    private void updateQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String qns_id = request.getParameter("qns_id");
        String question = request.getParameter("question");
        String status = request.getParameter("status");

        qFacade.updateQuestion(Long.parseLong(qns_id), question, status);

        json = gson.toJson(new JsonReturnMsg("Edit Question", "Question updated", "info"));
        response.getWriter().println(json);
    }

    private void createQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String question = request.getParameter("question");
        String theme = request.getParameter("theme");

        qFacade.createQuestion(Long.parseLong(theme), question);

        json = gson.toJson(new JsonReturnMsg("New Question", "Question created", "info"));
        response.getWriter().println(json);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
